// pages/rank/rank.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rank_list: [],
    rate_list: [],
    search_list: [],
    no_more_rank: false,
    no_more_rate: false,
    order_by_rank: true,
    show_search: false,
    rank_self: [],
    rate_self: [],
    gid: '',
    rank_page: 1,
    rate_page: 1,
    is_admin: 0,
    rank_key:0,
    rank_input_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    wx.getShareInfo({
      shareTicket: app.app_options.shareTicket,
      success: function (res) {
        wx.showLoading({
          title: '读取中',
        })
        app.request({
          url: app.root + 'rank',
          method: 'post',
          data: {
            encryptedData: res.encryptedData,
            iv: res.iv
          },
          success: function (res) {
            console.log(res)
            that.setData({
              rank_list: res.data.list,
              rank_self: res.data.self,
              is_admin: res.data.is_admin,
              rank_input_list: res.data.rank_input_list,
              gid: res.data.gid
            })
            wx.hideLoading()
          }
        })
      },
      fail: function (res) {
        console.log(res)

        wx.showLoading({
          title: '读取中',
        })
        app.request({
          url: app.root + 'rank',
          method: 'post',
          data: {
            encryptedData: res.encryptedData,
            iv: res.iv
          },
          success: function (res) {
            that.setData({
              // rank_list: res.data.list,
              rank_self: res.data.self,
              rate_self: res.data.self,
              is_admin: res.data.is_admin,
              rank_input_list: res.data.rank_input_list,
              gid: res.data.gid,
              no_more_rank: false,
              no_more_rate: false
            })
            wx.hideLoading()
          }
        })
      }
    })
  },

  onRankBtn: function (e) {
    const that = this
    that.setData({
      order_by_rank: true,
      show_search: false
    })

  },

  onRateBtn: function (e) {
    const that = this
    if (!that.data.gid) {
      that.setData({
        order_by_rank: false,
        show_search: false
      })
      return
    }

    if (!that.data.rate_list.length) {
      that.setData({
        order_by_rank: false,
        show_search: false
      })
      wx.showLoading({
        title: '读取中',
      })
      app.request({
        url: app.root + 'rank',
        method: 'post',
        data: {
          gid: this.data.gid,
          order_by_rank: false
        },
        success: function (res) {
          that.setData({
            rate_list: res.data.list,
            rate_self: res.data.self,
            page: that.data.rate_page
          })
          wx.hideLoading()
        }
      })
    } else {
      that.setData({
        order_by_rank: false,
        show_search: false
      })
    }
  },

  onAccuseBtn: function (e) {
    const uid = e.currentTarget.dataset.uid
    const nickname = e.currentTarget.dataset.nickname

    wx.showModal({
      title: '提示',
      content: '确定举报' + nickname + '吗？',
      success: function (res) {
        if (res.confirm) {
          app.request({
            url: app.root + 'rank/accuse',
            method: 'post',
            data: {
              uid: uid,
            },
            success: function (res) {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
              })
            }

          })
        }
      }
    })

  },

  onRankInput: function(e){
    const uid = e.currentTarget.dataset.uid
    const nickname = e.currentTarget.dataset.nickname
    const rank = e.currentTarget.dataset.rank
    this.setData({
      rank_input_id: uid,
      show_rank_input: true,
      rank_key: rank
    })

  },

  onRankConfirm: function(e){
    const uid = this.data.rank_input_id
    const rank = this.data.rank_key
    const that = this
    app.request({
      url: app.root + 'rank/update',
      method: 'post',
      data:{
        uid: uid,
        rank: rank
      },
      success: function(res){
        let rank_list = that.data.rank_list
        for(let i in rank_list){
          if(rank_list[i]['id'] == uid){
            rank_list[i]['rank'] = that.data.rank_input_list[rank]
            rank_list[i]['accuse_num'] = 0
          }
        }

        that.setData({
          rank_list : rank_list,
          show_rank_input: false
        })
        wx.showModal({
          title: '提示',
          content: '修改成功',
        })
      }

    })
  },

  onRankCancel: function(e){
    this.setData({
      show_rank_input: false
    })
  },

  onRankSelect: function(e){
    this.setData({
      rank_key: e.detail.value
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this
    if (that.data.order_by_rank && that.data.no_more_rank) {
      return
    }

    if (!that.data.order_by_rank && that.data.no_more_rate) {
      return
    }
    that.data.order_by_rank ? that.data.rank_page++ : that.data.rate_page++;
    wx.showLoading({
      title: '读取中',
    })
    app.request({
      url: app.root + 'rank',
      method: 'post',
      data: {
        gid: this.data.gid,
        order_by_rank: that.data.order_by_rank,
        page: that.data.order_by_rank ? that.data.rank_page : that.data.rate_page
      },
      success: function (res) {
        if (that.data.order_by_rank) {
          that.setData({
            no_more_rank: res.data.no_more,
            rank_list: that.data.rank_list.concat(res.data.list)
          })
        } else {
          that.setData({
            no_more_rate: res.data.no_more,
            rate_list: that.data.rate_list.concat(res.data.list)
          })
        }
        wx.hideLoading()
      }
    })
  },

  onSearchConfirm: function (e) {
    const that = this
    const word = e.detail.value
    if (!word.trim()) {
      return
    }
    app.request({
      url: app.root + 'rank/search',
      method: 'post',
      data: {
        gid: that.data.gid,
        word: word,
        order_by_rank: that.data.order_by_rank
      },
      success: function (res) {
        console.log(res)
        that.setData({
          show_search: true,
          search_list: res.data.list,

        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})